package com.supreme.cacheserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@RestController
@RequestMapping(value = "redis-set")
public class RedisSetController {

    @Resource
    private RedisTemplate<String, Serializable> redisTemplate;

    @Resource
    private List<RedisTemplate<String, Serializable>> redisTemplateList;

    @GetMapping(value = "add/{key}/{val}")
    public AjaxResp add(@PathVariable(value = "key") String key, @PathVariable(value = "val") String... val) {
        Long add = redisTemplate.opsForSet().add(key, val);
        return AjaxResp.instance(add);
    }

    @GetMapping(value = "remove/{key}/{val}")
    public AjaxResp remove(@PathVariable(value = "key") String key, @PathVariable(value = "val") Object... val) {
        Long remove = redisTemplate.opsForSet().remove(key, val);
        return AjaxResp.instance(remove);
    }

    @GetMapping(value = "members/{key}")
    public AjaxResp members(@PathVariable(value = "key") String key) {
        Set<Serializable> members = redisTemplate.opsForSet().members(key);
        return AjaxResp.instance(members);
    }

    @GetMapping(value = "isMember/{key}/{val}")
    public AjaxResp members(@PathVariable(value = "key") String key, @PathVariable(value = "val") Object... val) {
        Map<Object, Boolean> member = redisTemplate.opsForSet().isMember(key, val);
        return AjaxResp.instance(member);
    }

    @GetMapping(value = "randomMember/{key}")
    public AjaxResp randomMember(@PathVariable(value = "key") String key, @RequestParam(value = "count", required = false) Long count) {
        if (Objects.isNull(count)) {
            Serializable serializable = redisTemplate.opsForSet().randomMember(key);
            return AjaxResp.instance(serializable);
        } else {
            List<Serializable> serializableList = redisTemplate.opsForSet().randomMembers(key, count);
            return AjaxResp.instance(serializableList);
        }
    }

    @GetMapping(value = "distinctRandomMembers/{key}/{count}")
    public AjaxResp distinctRandomMembers(@PathVariable(value = "key") String key, @PathVariable(value = "count") long count) {
        Set<Serializable> serializables = redisTemplate.opsForSet().distinctRandomMembers(key, count);
        return AjaxResp.instance(serializables);
    }

    @GetMapping(value = "pop/{key}")
    public AjaxResp pop(@PathVariable(value = "key") String key, @RequestParam(value = "count", required = false) Long count) {
        if (Objects.isNull(count)) {
            Serializable pop = redisTemplate.opsForSet().pop(key);
            return AjaxResp.instance(pop);
        } else {
            List<Serializable> pop = redisTemplate.opsForSet().pop(key, count);
            return AjaxResp.instance(pop);
        }
    }

    @GetMapping(value = "difference/{key}/{keys}")
    public AjaxResp difference(@PathVariable(value = "key") String key, @PathVariable(value = "keys") List<String> otherKeys) {
        Set<Serializable> difference = redisTemplate.opsForSet().difference(key, otherKeys);
        return AjaxResp.instance(difference);
    }

    @GetMapping(value = "differenceAndStore/{keys}/{key}")
    public AjaxResp differenceAndStore(@PathVariable(value = "keys") List<String> keys, @PathVariable(value = "key") String key) {
        Long differenceAndStore = redisTemplate.opsForSet().differenceAndStore(keys, key);
        return AjaxResp.instance(differenceAndStore);
    }
}
